package org.flashbrighton.as3bots.models
{
	import __AS3__.vec.Vector;
	import flash.utils.Timer;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.Dictionary;

	import org.flashbrighton.as3bots.events.DeadBotEvent;
	
	
	public class GameModel extends EventDispatcher
	{		
		public static var MAX_TIME:int = 80;
		
		private var _isPlaying:Boolean;
		private var _started:Boolean;
		private var _bots:Vector.<BotModel>;
		private var _walls:Vector.<WallModel>;
		private var _physics:Physics;
		private var _timer:Timer;
		
		
		public function GameModel()
		{
			init();
			createWorld();
		}
		
		
		private function init():void
		{
			_bots = new Vector.<BotModel>();
			_walls = new Vector.<WallModel>();
			_isPlaying = false;
			_timer = new Timer(1000, MAX_TIME);
		}
		
		
		private function createWorld():void
		{
			_physics = new Physics(10, 10);
		}
		
		
		public function get isPlaying():Boolean
		{
			return _isPlaying;
		}
		
		
		public function set isPlaying(val:Boolean):void
		{
			_isPlaying = val;
		}
		
		
		public function get started():Boolean 
		{ 
			return _started; 
		}
		
		
		public function set started(val:Boolean):void 
		{
			_started = val;
		}
		
		
		public function get physics():Physics
		{
			return _physics;
		}
		
		
		public function get botList():Vector.<BotModel>
		{
			return _bots;
		}
		
		
		public function get walls():Vector.<WallModel>
		{
			return _walls;
		}
		
		
		public function get timer():Timer 
		{ 
			return _timer; 
		}
		
		
		public function addBot(botModel:BotModel):void
		{
			_bots.push(botModel);
		}
		
		
		public function removeBot(botModel:BotModel):void
		{
			_bots.splice(_bots.indexOf(botModel), 1);
			_physics.world.DestroyBody(botModel.body);
			dispatchEvent(new DeadBotEvent(DeadBotEvent.DEAD, botModel));
		}
		
		
		public function addWall(wallModel:WallModel):void
		{
			_walls.push(wallModel);
		}
		
		
		public function removeWall(wallModel:WallModel):void
		{
			_walls.splice(_walls.indexOf(wallModel), 1);
			_physics.world.DestroyBody(wallModel.body);
		}
		
		
		public function updateWorld():void
		{
			_physics.step();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
	}
}